﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MotivLogger
{
    public class Settings : ISetting
    {
        private static Dictionary<string, Settings> instance = null;
        private static SettingsProvider provider = new XmlSettings();
        private Dictionary<string, string> settings;
        public string ModuleName { get; set; }
        private static Object LockObj = new Object();

        public static Dictionary<string, Settings> modules { get { return instance; } }
        public Dictionary<string, string> items { get { return settings; } }
        private Dictionary<string, ISetting> childs;
        public Dictionary<string, ISetting> child { get { return childs; } }
        private static BaseCommunication CommunicationToolbox = null;

        private Settings(string ModuleName)
        {
            this.ModuleName = ModuleName;
            this.childs = new Dictionary<string, ISetting>(3);
            this.settings = provider.GetDictionary(ModuleName, this);
            if (this.settings.Count == 0 && this.child.Count == 0)
                throw new Exception("No settings for module '" + ModuleName + "' in configuration file(config.xml).");
        }
        public void AddChild(string ChildName)
        {
            Settings s = new Settings(this.ModuleName + "/" + ChildName);
            if (!instance.ContainsKey(this.ModuleName + "/" + ChildName))
                instance.Add(this.ModuleName + "/" + ChildName, s);
            this.childs.Add(ChildName, s);
        }
        public static Settings getSetting(string ModuleName)
        {
            lock (LockObj)
            {

                if (instance == null)
                {
                    instance = new Dictionary<string, Settings>();

                }
                try
                {
                    if (!instance.ContainsKey(ModuleName))
                        instance.Add(ModuleName, new Settings(ModuleName));
                    return instance[ModuleName];
                }
                catch (Exception e)
                {
                    MessageBox.Show("Error on loading settings for " + ModuleName + ". Original error message: " + e.Message);
                   
                }
                return null;
            }

        }

        public string GetData(string AttributeName)
        {
            return (this.settings.ContainsKey(AttributeName)) ? this.settings[AttributeName] : null;
        }

        public string GetStr(string AttributeName, string Default)
        {
            var value = this.GetData(AttributeName);
            if (value == null)
                value = Default;
            return value;
        }

        public bool GetBool(string AttributeName, bool Default)
        {
            var value = this.GetData(AttributeName);
            bool val;
            if (!bool.TryParse(value, out val))
                val = Default;
            return val;
        }

        public int GetInt(string AttributeName, int Default)
        {
            var value = this.GetData(AttributeName);
            int val;
            if (!Int32.TryParse(value, out val))
                val = Default;
            return val;
        }

        public double GetDouble(string AttributeName, double Default)
        {
            var value = this.GetData(AttributeName);
            double val;
            if (!Double.TryParse(value, out val))
                val = Default;
            return val;
        }

        public float GetFloat(string AttributeName, float Default)
        {
            var value = this.GetData(AttributeName);
            float val;
            if (!float.TryParse(value, out val))
                val = Default;
            return val;
        }

        public void setAttributeValue(string AttributeName, string NewValue)
        {
            if (settings.ContainsKey(AttributeName))
                this.settings[AttributeName] = NewValue;
        }

    }
}
